<template>
  <view class="index-wrapper"> </view>
</template>

<script setup>
import { ref, onMounted } from 'vue'

import { getUserInfoApi } from '@/api/user'
import { getLoginInfoStorageSync, setUserInfoStorageSync, getUserInfoStorageSync } from '@/utils/storageSync'

// 用户信息响应式数据
const userInfo = ref(null)

const getUserInfo = async () => {
  // 检查token是否存在
  const loginInfo = getLoginInfoStorageSync()

  if (!loginInfo.token) {
    uni.redirectTo({
      url: '/pages/login/index'
    })
    return
  }
  // 检查token是否过期
  if (loginInfo.expireTime) {
  }

  const res = await getUserInfoApi({ loading: true })
  // console.log('getUserInfo响应:', res)

  // 如果获取用户信息成功，存储到本地
  if (res && res.code === 0 && res.data) {
    setUserInfoStorageSync(res.data)
    userInfo.value = res.data
    // console.log('用户信息已存储到本地:', res.data)
  }
}

// 页面加载时检查本地是否已有用户信息
onMounted(() => {
  getUserInfo()
  const localUserInfo = getUserInfoStorageSync()
  if (localUserInfo) {
    userInfo.value = localUserInfo
    // console.log('从本地加载用户信息:', localUserInfo)
  }
})
</script>

<style lang="scss" scoped></style>
